package org.lex.utils.collections;

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Iterator;

public class ArrayListStack<T> implements Stack<T> {
	private ArrayList<T> list = null;

	public ArrayListStack() {
		super();
		this.list = new ArrayList<T>();
	}

	public T peek() {
		if (true == list.isEmpty())
			throw new EmptyStackException();
		return list.get(list.size() - 1);
	}

	public T pop() {
		if (true == list.isEmpty())
			throw new EmptyStackException();
		return list.remove(list.size() - 1);
	}

	public void push(T e) {
		list.add(e);
	}

	public void push(Iterator<T> itor) {
		while (itor.hasNext())
			list.add(itor.next());
	}

	public void push(T... array) {
		if (null == array || 0 == array.length)
			return;
		for (T e : array)
			list.add(e);
	}

	public void clear() {
		this.list.clear();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public int size() {
		return list.size();
	}

	public Iterator<T> iterator() {
		return this.list.iterator();
	}
}
